// const BASE_URL = 'http://redcross.api.zhongzhixinsd.com'
// const BASE_URL = 'http://39.91.138.170:8088'
const BASE_URL = 'https://szhh.zaozhuangredcross.org.cn'
export const imageUrl = 'https://puqinzc.oss-cn-beijing.aliyuncs.com/'

// 处理未授权状态（不跳转到登录页）
const handleUnauthorized = () => {
  // 清除登录信息但不跳转
  try {
    uni.removeStorageSync('user_token')
    uni.removeStorageSync('user_info')
    uni.removeStorageSync('login_time')
  } catch (e) {
    console.error('清除登录信息失败:', e)
  }
}
export const uploadFile = (path, fileType = 'image') => {
	// 调接口加载

	return new Promise((resolve, reject) => {
		
		
		const requestUrl = BASE_URL + '/oss/apiUpload'
		const header = {
			'token': uni.getStorageSync('user_token'),
            'Authorization': uni.getStorageSync('user_token'),
			'content-type': "multipart/form-data"
		};
		uni.showLoading({
			title: '上传中'
		})
		uni.uploadFile({
			url: requestUrl,
			// fileType: fileType,
			name: 'file',
			filePath: path,
			// formData: {
			// 	biz: 'temp'
			// },
			// header,
			success: (res) => {
				if (res.statusCode === 200) {
					let resData = JSON.parse(res.data)
					if (resData.code == 401) {
						// 处理401未授权状态
						handleUnauthorized()
						reject(resData)
					} else {
						resolve(resData.data)
					}
				} else if (res.statusCode === 401) {
					// 处理401未授权状态
					handleUnauthorized()
					reject(res)
				} else {
					uni.showToast({
						title: '上传失败, 请稍后再试',
						icon: "none"
					})
					setTimeout(() => {
						reject(res)
					}, 1500)
				}
			},
			fail: (error) => {
				// uni.hideLoading();
				uni.showToast({
					title: "系统异常，请稍后再试",
					icon: "none"
				})
			},
			complete: () => {
				// uni.hideLoading();
			},
		});
	})
}